package com.helen.exam03.Service;/*
 *@Author: helen
 *@Date:   2021/5/11 15:43
 *@Description:
 */

import com.helen.exam03.Config.CsvConfig;
import com.helen.exam03.Entity.RegionStats;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DataService {

    //获取全部地区的数据
    public List<RegionStats> getAllData(){
        return CsvConfig.list;
    }

    //通过关键字查询地区数据
    public List<RegionStats> FindRegionDataByKey(String key){
        return CsvConfig.list.stream().filter((s) ->s.getCountry().equals(key))
                .collect(Collectors.toList());
    }

    //查询中国的数据
    public List<RegionStats> FindChineseData(){
        return CsvConfig.list.stream().filter((s) ->s.getCountry().equals("中国"))
                .collect(Collectors.toList());
    }

    //查询严重前10地区
    public List<RegionStats> findTop10(){
        return CsvConfig.list.stream().limit(10).collect(Collectors.toList());
    }


    //获取单个国家感染人数
    public int getSumOne(String country){
        int sum = 0;
        for (int i = 0; i < CsvConfig.list.size(); i++) {
            if (CsvConfig.list.get(i).getCountry().equals(country))
                sum += CsvConfig.list.get(i).getLatestLocalCases();
        }
        return sum;
    }

    //获取全球感染人数
    public int getSum(){
        int sum = CsvConfig.list.stream().mapToInt(RegionStats::getLatestLocalCases).sum();
        return sum;
    }

    //获取单个国家新增人数
    public int getDiffOne(String country){
        int sum = 0;
        for (int i = 0; i < CsvConfig.list.size(); i++) {
            if (CsvConfig.list.get(i).getCountry().equals(country))
                sum += CsvConfig.list.get(i).getDiffFormPrevDay();
        }
        return sum;
    }

    //获取全球新增数量
    public int getDiffData(){
        int sum = CsvConfig.list.stream().mapToInt(RegionStats::getDiffFormPrevDay).sum();
        return sum;
    }
}
